import 'package:flutter/material.dart';
import 'package:wallet_utils/log_util.dart';
String rootRoute = "/";

class HTNavigator {

  ///route 为返回页面的route名称
  static pop<T extends Object>(BuildContext context,{String? route,T? result}) {
    if (route != null) {
      LogUtils.d(ModalRoute.withName(route));
      Navigator.popUntil(context, (Route route1) {
        LogUtils.d(route1.settings.name);
        return (route1.settings.name == route);
      });
      return;
    }
    Navigator.of(context).pop(result);
  }

  static Future push(BuildContext context,Widget page,{bool? fullscreenDialog}) {

    return Navigator.push(
      context,
      MaterialPageRoute(
          fullscreenDialog: fullscreenDialog ?? false,
          builder: (context) => page),
    );
  }

  static Future pushWithName(BuildContext context,Widget page,{String? routeName}) {

    return Navigator.push(
      context,
      MaterialPageRoute(
          settings: RouteSettings(name: routeName ?? page.runtimeType.toString()),
          builder: (context) => page),
    );
  }

  static Future pushReplacement(BuildContext context,Widget page,{String? routeName}) {
    return Navigator.pushReplacement(
      context,
      MaterialPageRoute(
          settings: RouteSettings(name: routeName ?? page.runtimeType.toString()),
          builder: (context) => page),
    );
  }

  static Future pushAndRemoveUntil(BuildContext context,Widget page,String untilRouteName,{String? routeName}) {
    return Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
        settings: RouteSettings(name: routeName ?? page.runtimeType.toString()),
        builder: (context) => page), (Route route1) {
      LogUtils.d(route1.settings.name);
      return (route1.settings.name == untilRouteName);
    });
  }


}
